package com.example.demo1.myweb;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    //http://127.0.0.1:8080/sayGoods?good=pen
    @RequestMapping("/sayGoods")
    @ResponseBody
    public String hello(@RequestParam(name = "good", defaultValue = "unknown goods") String good) {
        return "Hello! this goods is:\n" + good;
    }

    //http://127.0.0.1:8080/goods
    @RequestMapping("/goods")
    @ResponseBody
    public Goods goods(){
        Goods g = new Goods("book",100);
        return g;
    }

    //http://127.0.0.1:8080/save_goods
    @RequestMapping("/save_goods")
    @ResponseBody
    public String saveUser(Goods g) {
        return "user will save: name=" + g.getName() + ", price=" + g.getPrice();
    }

    //http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String html(){
        return "goods.html";
    }

    @ModelAttribute
    public void parseGoods(@RequestParam(name = "goods", defaultValue = "unknown goods") String name
            , @RequestParam(name = "price", defaultValue = "12") Integer price, Goods goods) {
        goods.setName("lollipop");
        goods.setPrice(18);
    }

}
